#include <stdio.h>

int main(int argc, char *argv[]) 
{
	int k, n, sum, m;
	scanf("%d %d", &k, &n);
	m = 1; sum = 0;
	while (n) {
		if(n % 2){
			sum+=m;
		}
		n >>= 1;
		m*=k;
	}
	printf("%d\n",sum);
	return 0;
}

